package com.yida.gwj.config.controller;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.api.domain.GwjConfCalculateData;
import com.yida.gwj.config.domain.dto.GwjConfCalculateDataDTO;
import com.yida.gwj.config.service.IGwjConfCalculateDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 计算金额算法Controller
 * <p>
 * 修订20230112 hmh(修改地址，权限名,方法名替换，提示信息修改，修改新增、修改时判定当前计算金额未对金额做校验导致出现多条数据异常)
 *
 * @author lqq
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/calculateData")
public class GwjConfCalculateDataController extends BaseController {
	@Autowired
	private IGwjConfCalculateDataService gcCalculateDataService;

	/**
	 * 查询计算金额算法列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("gwj:config:calculateData:list")
	public AjaxResult list(GwjConfCalculateData gcCalculateData) {
		List<GwjConfCalculateData> list = gcCalculateDataService.selectGwjConfCalculateDataList(gcCalculateData);
		return AjaxResult.success(GwjConvertUtil.convertList(list, GwjConfCalculateDataDTO.class));
	}

	/**
	 * 导出计算金额算法列表
	 */
	@PostMapping("/export")
	@RequiresPermissions("gwj:config:calculateData:export")
	@Log(title = "计算金额算法", businessType = BusinessType.EXPORT, systemName = "gwj")
	public void export(HttpServletResponse response, GwjConfCalculateData gcCalculateData) {
		List<GwjConfCalculateData> list = gcCalculateDataService.selectGwjConfCalculateDataList(gcCalculateData);
		ExcelUtil<GwjConfCalculateData> util = new ExcelUtil<>(GwjConfCalculateData.class);
		util.exportExcel(response, list, "计算金额算法数据");
	}

	/**
	 * 获取计算金额算法详细信息
	 */
	@GetMapping(value = "/{dataId}")
	@RequiresPermissions("gwj:config:calculateData:query")
	public AjaxResult getInfo(@PathVariable("dataId") Long dataId) {
		return AjaxResult.success(gcCalculateDataService.selectByCalculateDataId(dataId));
	}

	/**
	 * 新增计算金额算法
	 */
	@PostMapping
	@RequiresPermissions("gwj:config:calculateData:add")
	@Log(title = "计算金额算法", businessType = BusinessType.INSERT, systemName = "gwj")
	public AjaxResult add(@RequestBody GwjConfCalculateData gcCalculateData) {
		return toAjax(gcCalculateDataService.insertGwjConfCalculateData(gcCalculateData));
	}

	/**
	 * 修改计算金额算法
	 */
	@PutMapping
	@RequiresPermissions("gwj:config:calculateData:edit")
	@Log(title = "计算金额算法", businessType = BusinessType.UPDATE, systemName = "gwj")
	public AjaxResult edit(@RequestBody GwjConfCalculateData gcCalculateData) {
		return toAjax(gcCalculateDataService.updateGwjConfCalculateData(gcCalculateData));
	}

	/**
	 * 删除计算金额算法
	 */
	@DeleteMapping("/{dataIds}")
	@RequiresPermissions("gwj:config:calculateData:remove")
	@Log(title = "计算金额算法", businessType = BusinessType.DELETE, systemName = "gwj")
	public AjaxResult remove(@PathVariable Long[] dataIds) {
		return toAjax(gcCalculateDataService.deleteByGwjConfCalculateDataIds(dataIds));
	}

	/**
	 * 根据标准类型ID、地区ID、楼栋类型查询计算标准值数据
	 */
	@InnerAuth
	@GetMapping("/inner/{calculateTypeId}/{areaId}/{buildTypeId}")
	public R<GwjConfCalculateData> selectCalculateDataByCalculateTypeIdAndAreaIdAndBuildTypeId(@PathVariable("calculateTypeId") Long calculateTypeId, @PathVariable("areaId") Long areaId, @PathVariable("buildTypeId") Integer buildTypeId) {
		return R.ok(gcCalculateDataService.selectCalculateDataByCalculateTypeIdAndAreaIdAndBuildTypeId(calculateTypeId, areaId, buildTypeId));
	}

}
